<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>子访问父元素</title>
    <cpn></cpn>
</head>
<body>
<div id="app">
    <h1>{{message}}</h1>
    <cpn></cpn>
</div>
<template id="cpn">
    <div>
        <h1>子组件</h1>
        <button v-on:click="btnClick">子访问父</button>
    </div>
</template>
<script src="../../js/vue.js"></script>
<script>
    const cpnc = ({
        template: "#cpn",
        methods: {
            btnClick() {
                //1.子访问父：$parent
                console.log(this.$parent);
                //2.子访问根元素：$root
                console.log(this.$root);
            }
        }
    });
    new Vue({
        el: "#app",
        data: {
            message: "你好，世界",
        },
        components: {
            cpn: cpnc,
        }
    })
</script>
</body>
</html>